/**  

* @author renbangjie renbangjie@126.com  
* @version V1.0  
*/
package com.ai.service.reviewed.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.ai.common.core.base.DTO;
import com.ai.common.core.mapper.Mapper;
import com.ai.common.core.pager.model.PagerUtil;
import com.ai.common.core.pager.service.IPagerService;
import com.ai.common.core.service.impl.BaseServiceImpl;
import com.ai.common.core.utils.CollectionUtils;
import com.ai.common.model.Pagination;
import com.ai.mmd.reviewed.dto.SbhzinfoDTO;
import com.ai.mmd.reviewed.dto.SbhzinfoExtDTO;
import com.ai.mmd.reviewed.dto.SbmxinfoDTO;
import com.ai.mmd.reviewed.dto.StatisticsDTO;
import com.ai.mmd.reviewed.dto.StatisticsHwlxDTO;
import com.ai.mmd.reviewed.dto.YhxxDTO;
import com.ai.mmd.reviewed.vo.HotmapVO;
import com.ai.mmd.reviewed.vo.PreviewinfoVO;
import com.ai.mmd.reviewed.vo.SbhzinfoVO;
import com.ai.mmd.reviewed.vo.SbmxinfoExtVO;
import com.ai.mmd.reviewed.vo.SbmxinfoVO;
import com.ai.mmd.reviewed.vo.StatisticsHwlxVO;
import com.ai.mmd.reviewed.vo.StatisticsVO;
import com.ai.mmd.reviewed.vo.YhxxVO;
import com.ai.mmd.system.dto.GkcityRelationDTO;
import com.ai.mmd.system.vo.GkcityRelationVO;
import com.ai.service.reviewed.dao.SbhzinfoModelMapper;
import com.ai.service.reviewed.dao.SbmxinfoModelMapper;
import com.ai.service.reviewed.dao.YhxxModelMapper;
import com.ai.service.reviewed.model.SbhzinfoModel;
import com.ai.service.reviewed.model.SbmxinfoModel;
import com.ai.service.reviewed.model.YhxxModel;
import com.ai.service.reviewed.service.ISbhzinfoService;
import com.ai.service.system.dao.GkcityRelationModelMapper;
import com.ai.service.system.model.GkcityRelationModel;

/**
* 
*	登陆信息的业务实现类
* 
* @author renbangjie renbangjie@126.com
*/
@Service("isbhzinfoService")
public class SbhzinfoServiceImpl extends BaseServiceImpl<SbhzinfoModel>
		implements ISbhzinfoService {
	
	/**
     * 详细信息的分页
     *
     * @param yhxxDTO
     * @return
     * @throws Exception
     */
    @Override
    public Pagination<SbhzinfoVO> getListVO(SbhzinfoDTO sbhzinfoDTO) throws Exception {
        return PagerUtil.getPagination(new IPagerService<SbhzinfoVO>() {
            @Override
            public int queryCount(DTO dto) throws Exception {
                return getMapper().getListCount(dto);
            }

            @Override
            public List<SbhzinfoVO> queryList(DTO dto) throws Exception {
                List<SbhzinfoVO> voList = new ArrayList<SbhzinfoVO>();
                List<SbhzinfoModel> shhzList = getMapper().getList(dto);
                if (shhzList.isEmpty()) {
                    return voList;
                }
                //会员信息
                YhxxDTO yhxxDTO = new YhxxDTO();
                List<String> yhidList = CollectionUtils
                        .getBeanFieldValues(shhzList, "yhid");
                yhxxDTO.setYhidList(yhidList);
                Map<String, YhxxModel> yhxxMap = CollectionUtils
                        .toMap(yhxxModelMapper.getList(yhxxDTO), "yhid");

                SbhzinfoVO sbhzinfoVO = null;
                YhxxVO yhxxVO = null;

                for (SbhzinfoModel sbhzinfoModel : shhzList) {
                	sbhzinfoVO = new SbhzinfoVO();
                    BeanUtils.copyProperties(sbhzinfoModel, sbhzinfoVO);
                    if (yhxxMap != null && !yhxxMap.isEmpty() && yhxxMap
                            .containsKey(sbhzinfoModel.getYhid())) {
                    	yhxxVO = new YhxxVO();
                        BeanUtils.copyProperties(yhxxMap.get(sbhzinfoModel.getYhid()),
                        		yhxxVO);
                        sbhzinfoVO.setYhxxVO(yhxxVO);
                    }
                    voList.add(sbhzinfoVO);
                }
                return voList;
            }
        }, sbhzinfoDTO);
    }
    
    /**
	 * 查询货物运单号列表
	 * @param SbhzinfoDTO
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, String>> getHwydhList(SbhzinfoDTO sbhzinfoDTO){
		return sbhzinfoModelMapper.getHwydhList(sbhzinfoDTO);
	}
	
	/**
	 * 查询铁路货票号列表
	 * @param SbhzinfoDTO
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, String>> getTlhphList(SbhzinfoDTO sbhzinfoDTO){
		return sbhzinfoModelMapper.getTlhphList(sbhzinfoDTO);
	}
	
	/**
	 * 箱量统计
	 * @param StatisticsDTO
	 * @return
	 * @throws Exception
	 */
	public List<StatisticsVO> getXlStatistics(StatisticsDTO statisticsDTO){
		return sbhzinfoModelMapper.getXlStatistics(statisticsDTO);
	}
	
	/**
     * 货物类型统计
     * @param StatisticsHwlxDTO
     * @return
     * @throws Exception
     */
    public List<StatisticsHwlxVO> getHwlxStatistics(StatisticsHwlxDTO statisticsHwlxDTO)
            throws Exception{
    	return sbhzinfoModelMapper.getHwlxStatistics(statisticsHwlxDTO);
    }
    
    /**
	 * 获得港口作业记录
	 * @param SbhzinfoDTO
	 * @return
	 * @throws Exception
	 */
    public List<SbmxinfoExtVO> getsbgkList(SbhzinfoExtDTO sbhzinfoExtDTO) throws Exception {
    	return sbhzinfoModelMapper.getsbgkList(sbhzinfoExtDTO);
    }
    
    /**
     * 查询申报站点数据列表
     * @param SbhzinfoExtDTO
     * @return
     * @throws Exception
     */
    public List<SbmxinfoExtVO> getsbzdList(SbhzinfoExtDTO sbhzinfoExtDTO)
            throws Exception{
    	return sbhzinfoModelMapper.getsbzdList(sbhzinfoExtDTO);
    }
    
    /**
     * 起点终点统计
     * @param StatisticsDTO
     * @return
     * @throws Exception
     */
	public List<StatisticsVO> getfromtoStatistics(StatisticsDTO statisticsDTO){
		return sbhzinfoModelMapper.getfromtoStatistics(statisticsDTO);
	}
	
	/**
     * 根据汇总单号查询箱号明细
     * @param StatisticsDTO
     * @return
     * @throws Exception
     */
	public Map<String, Integer> getxhDetail(Map<String, Object> map){
		List<Map<String, Object>> list = sbhzinfoModelMapper.getxhDetail(map);
		
		Integer xl = 0;
		Integer cash = 0;
		List<String> jdList = new ArrayList<String>();
		
		for (Map<String, Object> mapdetail : list) {
			//String tempxh = mapdetail.get("xh").toString();
			String tempxx = mapdetail.get("xxmc").toString();
			Integer bz = Integer.parseInt(mapdetail.get("bz").toString());
			String tempjd = mapdetail.get("sbjd").toString();
			
			if ("2".equals(tempxx.substring(0, 1))) {
				xl += 1;
				cash += 1*bz;
			}else {
				xl += 2;
				cash += 2*bz;
			}
			
			if (!jdList.contains(tempjd)) {
				jdList.add(tempjd);
			}
		}
		
		Map<String, Integer> returnmap = new HashMap<String, Integer>();
		returnmap.put("xl", xl);
		returnmap.put("cash", cash);
		returnmap.put("jd", jdList.size());
		
		return returnmap;
	}
	
	/**
     * 获得比对有质疑的明细总数
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public Integer getCompareWorngListCount(SbhzinfoDTO sbhzinfoDTO)throws Exception{
		return sbhzinfoModelMapper.getCompareWorngListCount(sbhzinfoDTO);
	}
	
	/**
     * 更新对比有质疑的汇总和明细
     * @param List<SbmxinfoVO>
     * @return
     * @throws Exception
     */
	public void updatehzmx(List<SbmxinfoVO> list)throws Exception{
		//更新明细
		List<String> hzdhlist = new ArrayList<String>();
		SbmxinfoModel sbmxinfoModel = null;
		SbhzinfoDTO sbhzinfoDTO = null;
		for (SbmxinfoVO sbmxinfoVO : list) {
			//获得汇总单号集合
			if (!hzdhlist.contains(sbmxinfoVO.getHzdh())) {
				hzdhlist.add(sbmxinfoVO.getHzdh());
			}
			
			//更新明细状态和原因
			sbmxinfoModel = new SbmxinfoModel();
			BeanUtils.copyProperties(sbmxinfoVO,sbmxinfoModel);
			sbmxinfoModel.setIshc(2);
			sbmxinfoModel.setJjyy("数据冲突");
			sbmxinfoModel.setGxsj(new Date());
			sbmxinfoModelMapper.updateByPrimaryKey(sbmxinfoModel);
		}
		
		for (String hzdh : hzdhlist) {
			sbhzinfoDTO = new SbhzinfoDTO();
			sbhzinfoDTO.setHzdh(hzdh);
			SbhzinfoModel sbhzinfoModel = sbhzinfoModelMapper.getOne(sbhzinfoDTO);
			if (sbhzinfoModel!=null) {
				sbhzinfoModel.setHzzt(4);
				sbhzinfoModel.setGxsj(new Date());
				sbhzinfoModelMapper.updateByPrimaryKey(sbhzinfoModel);
			}
		}
	}
	
	/**
     * 更新无质疑的汇总和明细
     * @param List<SbmxinfoVO>
     * @return
     * @throws Exception
     */
	public void updaterighthzmx(SbhzinfoDTO sbhzinfoDTO)throws Exception{
		
		List<String> hzdhlist = new ArrayList<String>();
		//更新汇总
		SbhzinfoDTO hzdto = new SbhzinfoDTO();
		hzdto.setSbnf(sbhzinfoDTO.getSbnf());
		hzdto.setSbjd(sbhzinfoDTO.getSbjd());
		hzdto.setHzzt(1);
		List<SbhzinfoModel> hzmodellist = sbhzinfoModelMapper.getList(hzdto);
		SbmxinfoModel sbmxinfoModel = null;
		
		if (hzmodellist.size()>0) {
			for (SbhzinfoModel sbhzinfoModel : hzmodellist) {
				if (!hzdhlist.contains(sbhzinfoModel.getHzdh())) {
					hzdhlist.add(sbhzinfoModel.getHzdh());
				}
				
				sbhzinfoModel.setHzzt(2);
				sbhzinfoModelMapper.updateByPrimaryKey(sbhzinfoModel);
			}
			
			for (String hzdh : hzdhlist) {
				sbmxinfoModel = new SbmxinfoModel();
				sbmxinfoModel.setHzdh(hzdh);
				sbmxinfoModel.setIshc(1);
				sbmxinfoModelMapper.updateByPrimaryKey(sbmxinfoModel);
			}
		}
	}
	
	/**
     * 更新所有该季度的数据为通过
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public void crossAllHz(SbhzinfoDTO sbhzinfoDTO)throws Exception {
		
		List<SbhzinfoModel> list = sbhzinfoModelMapper.getList(sbhzinfoDTO);
		if (list.size()>0) {
			for (SbhzinfoModel sbhzinfoModel : list) {
				sbhzinfoModel.setHzzt(2);
				sbhzinfoModel.setGxsj(new Date());
				sbhzinfoModelMapper.updateByPrimaryKey(sbhzinfoModel);
			}
		}
	}
	
	/**
     * 首页统计总箱量和总金额
     * @param 
     * @return
     * @throws Exception
     */
	public List<SbhzinfoVO> getStatisticsForIndex(Map<Object, Integer> map) throws Exception{
		return sbhzinfoModelMapper.getStatisticsForIndex(map);
	}
	
	/**
     * 根据汇总单号批量更新明细状态
     * @param SbhzinfoModel
     * @return
     * @throws Exception
     */
	public void updateMxBatch(SbhzinfoModel sbhzinfoModel,Integer flag) throws Exception{
		
		sbhzinfoModelMapper.updateByPrimaryKey(sbhzinfoModel);
		
		/*String hzdh = sbhzinfoModelMapper.getByPrimaryKey(sbhzinfoModel.getId()).getHzdh();
		
		SbmxinfoDTO sbmxinfoDTO = new SbmxinfoDTO();
		sbmxinfoDTO.setHzdh(hzdh);
		List<SbmxinfoModel> list = sbmxinfoModelMapper.getList(sbmxinfoDTO);
		for (SbmxinfoModel sbmxinfoModel : list) {
			if (flag!=2) {
				sbmxinfoModel.setJjyy("");
			}
			sbmxinfoModel.setIshc(flag);
			sbmxinfoModel.setGxsj(new Date());
			sbmxinfoModelMapper.updateByPrimaryKey(sbmxinfoModel);
		}*/
	}
	
	/**
     * 获得该年度有申报记录的企业
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public List<YhxxVO> getCompanyList(SbhzinfoDTO sbhzinfoDTO)throws Exception{
		return sbhzinfoModelMapper.getCompanyList(sbhzinfoDTO);
	}
	
	/**
     * 获得该年度有申报记录的企业
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public StatisticsVO getALLStatistics(SbhzinfoDTO sbhzinfoDTO)throws Exception{
		return sbhzinfoModelMapper.getALLStatistics(sbhzinfoDTO);
	}
	
	/**
     * 查询预览列表
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public PreviewinfoVO getPreviewList(SbhzinfoDTO sbhzinfoDTO)throws Exception{
		return sbhzinfoModelMapper.getPreviewList(sbhzinfoDTO);
	}
	
	/**
     * 查询热力数据列表
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public List<HotmapVO> getHotmapList(SbhzinfoDTO sbhzinfoDTO)throws Exception {
		
		List<HotmapVO> hList = new ArrayList<HotmapVO>();
		HotmapVO hotmapVO = null;
		
		//1.获得关联数据
		GkcityRelationDTO gkcityRelationDTO = new GkcityRelationDTO();
		gkcityRelationDTO.setSfsc(0);
		List<GkcityRelationVO> dislist = gkcityRelationModelMapper.getdisList(gkcityRelationDTO);
		
		//2.循环关联城市数据
		for (GkcityRelationVO gkcityRelationVO : dislist) {
			gkcityRelationDTO = new GkcityRelationDTO();
			gkcityRelationDTO.setCityid(gkcityRelationVO.getCityid());
			gkcityRelationDTO.setSfsc(0);
			List<GkcityRelationModel> regklist = gkcityRelationModelMapper.getList(gkcityRelationDTO);
		
			Integer citycount = 0;
			//3.循环港口数据
			for (GkcityRelationModel gkcityRelationModel2 : regklist) {
				sbhzinfoDTO.setGkid(gkcityRelationModel2.getGkid());
				//4.根据港口条件和查询条件获得港口总箱量
				StatisticsVO statisticsVO = sbhzinfoModelMapper.gethotmapList(sbhzinfoDTO);
				if (statisticsVO!=null) {
					citycount += statisticsVO.getCount();
				}
			}
			
			//5.向pojo设置值
			if (citycount==0) {
				continue;
			}
			hotmapVO = new HotmapVO();
			hotmapVO.setName(gkcityRelationVO.getCity());
			hotmapVO.setValue(citycount);
			
			//6.添加到集合
			hList.add(hotmapVO);
		}
		return hList;
	}
	
	/**
     * 查询时间错误数据
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public List<SbmxinfoVO> getTimeErrorList(SbhzinfoDTO sbhzinfoDTO)throws Exception{
		return sbhzinfoModelMapper.getTimeErrorList(sbhzinfoDTO);
	}
	
	/**
     * 更新时间错误列表
     * @param SbhzinfoDTO
     * @return
     * @throws Exception
     */
	public void updateWrongTimeList(List<SbmxinfoVO> mxlist)throws Exception{
		SbhzinfoDTO sbhzinfoDTO = null;
		SbhzinfoModel sbhzinfoModel = null;
		SbmxinfoModel sbmxinfoModel = null;
		for (SbmxinfoVO mx : mxlist) {
			//更新汇总
			sbhzinfoDTO = new SbhzinfoDTO();
			sbhzinfoDTO.setHzdh(mx.getHzdh());
			sbhzinfoDTO.setHzzt(1);
			sbhzinfoModel = sbhzinfoModelMapper.getOne(sbhzinfoDTO);
			if (sbhzinfoModel!=null) {
				sbhzinfoModel.setHzzt(4);
				sbhzinfoModel.setGxsj(new Date());
				sbhzinfoModelMapper.updateByPrimaryKey(sbhzinfoModel);
			}
			
			//更新明细
			sbmxinfoModel = sbmxinfoModelMapper.getByPrimaryKey(mx.getId());
			if (sbmxinfoModel!=null) {
				sbmxinfoModel.setIshc(2);
				sbmxinfoModel.setJjyy("跨季度申报");
				sbmxinfoModelMapper.updateByPrimaryKey(sbmxinfoModel);
			}
		}
	}

	@Resource(name = "sbhzinfoModelMapper")
	private SbhzinfoModelMapper sbhzinfoModelMapper;
	
	@Resource(name = "sbmxinfoModelMapper")
	private SbmxinfoModelMapper sbmxinfoModelMapper;
	
	@Resource(name = "yhxxModelMapper")
	private YhxxModelMapper yhxxModelMapper;
	
	@Resource(name = "gkcityRelationModelMapper")
	private GkcityRelationModelMapper gkcityRelationModelMapper;
	
	@Override
	protected Mapper<SbhzinfoModel> getMapper() {
		return sbhzinfoModelMapper;
	}
}
